<?php
//百融
namespace App\Services;
class BairongService
{
    private $apiCode = '';//账户标识
    private $appKey = '';//appKey 标识
    private $strategy_id = '';//贷前策略编号
    private $conf_id = '';//信息验证策略编号
    private $strategy_api_url = '';//贷前策略 APIurl
    private $infoverify_api_url = '';//信息验证 API APIurl
    private $username = '';//账号
    private $password = '';//密码

    public function __construct()
    {
        $data = config('boolean');
//        var_dump($data);exit;
        $this->apiCode = $data['apiCode'];
        $this->appKey = $data['apiKey'];
//        $this->strategy_id = $data['bairong_strategy_id'];
//        $this->conf_id = $data['bairong_conf_id'];
        $this->username = $data['username'] ?? "ztkj5Str";
        $this->password = $data['password'] ?? "mQrD844@";

        $this->strategy_api_url = $data['strategy_api_url'];//贷前策略
        $this->infoverify_api_url = $data['infoverify_api_url'];//信息验证


    }

    public function getStrategyData($param, $strategy_id)
    {

        $apiCode = $this->apiCode;
        $appKey = $this->appKey;
        $appKey_hash = hash("md5", $appKey);
        $data = [
            'id' => hash("md5", $param['id_card']),//身份证号码
            'cell' => hash("md5", $param['mobile']),//手机号
            'name' => $param['name'],//姓名
            'strategy_id' => $strategy_id,
        ];
        $jsonDataCode = $jsonData = stripslashes(json_encode($data, JSON_UNESCAPED_UNICODE));
        $jsonDataAES = $this->AESEncrypt($jsonDataCode, $appKey);
        $checkCodeHash = hash("md5", $jsonDataCode . $apiCode . $appKey);
        $post = array(
            'apiCode' => $apiCode,
            'appKey' => $appKey_hash,
            'jsonData' => $jsonDataAES,
            'checkCode' => $checkCodeHash,
        );

        $strategyData = $this->requestCurl($post, $this->strategy_api_url);

        $strategyData = json_decode($strategyData, true);
        \Log::channel('bairong')->info("策略ID:{$strategy_id}",[$param,$strategyData]);
        return $strategyData;
    }

    public function getInfoverifyData($param, $strategy_id)
    {

        $apiCode = $this->apiCode;
        $appKey = $this->appKey;
        $appKey_hash = hash("md5", $appKey);
        $mobile = isset($param['mobile']) ? $param['mobile'] : '';
        $data = [
            'id' => hash("md5", $param['id_card']),//身份证号码
            'cell' => hash("md5", $mobile),//手机号
            'name' => $param['name'],//姓名
            'conf_id' => $strategy_id,
        ];
        $jsonDataCode = $jsonData = stripslashes(json_encode($data, JSON_UNESCAPED_UNICODE));
        $jsonDataAES = $this->AESEncrypt($jsonDataCode, $appKey);
        $checkCodeHash = hash("md5", $jsonDataCode . $apiCode . $appKey);
        $post = array(
            'apiCode' => $apiCode,
            'appKey' => $appKey_hash,
            'jsonData' => $jsonDataAES,
            'checkCode' => $checkCodeHash,
        );

        $strategyData = $this->requestCurl($post, $this->infoverify_api_url);
        $strategyData = json_decode($strategyData, true);
        \Log::channel('bairong')->info("验证ID:{$strategy_id}",[$param,$strategyData]);

        return $strategyData;
    }




    /**
     * 获取token
     * @return array
     */
    public function getToken()
    {


        $file = "/boolean/bairong";
        $url = 'https://apiservice.100credit.com/api/auth-service/auth/getToken';
        $post = [
            'username' => $this->username,
            'password' => $this->password,
        ];
        $info = $this->requestCurl($post, $url);
        $info = json_decode($info, true);
//        com('log')->write($file, ['百融token', $info]);
        if ($info['code'] == "000000") {
            $token = $info['data'];
        } else {
            $token = '';
        }
        return $token;
    }





    /**
     * @param $data
     * @param $url
     * @return bool|string
     */
    public function requestCurl($post, $url)
    {

//        $url = "https://sandbox-api2.100credit.cn/strategy_api/v3/hx_query";
        $ch = curl_init();
        $httpheader[] = "content-type:application/x-www-form-urlencoded";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    public function AESEncrypt($data, $key)
    {
        $key = $this->SHA1PRNG($key);
        $result = openssl_encrypt($data, "AES-128-ECB", $key, OPENSSL_RAW_DATA);
        return $this->EncodeBase64URLSafeString($result);
    }

    public function SHA1PRNG($key)
    {
        return substr(openssl_digest(openssl_digest($key, "sha1", true), "sha1", true), 0, 16);
    }

    public function EncodeBase64URLSafeString($result)
    {
        return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($result));
    }
}
